package com.drone.drone_server.controller;

import com.drone.drone_server.api.ApiResponse;
import com.drone.drone_server.dao.*;
import com.drone.drone_server.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/fires")
class FireController {

    @Autowired
    private FireVideoDao fireVideoDao;
    @Autowired
    private PersonFireHandlingDao personFireHandlingDao;

    // 处理火情
    @PutMapping("/handle")
    public ApiResponse handleFire(@RequestParam Integer fireId,
                                  @RequestParam String fireLevel,
                                  @RequestParam Long personPhoneNumber) {
        FireVideo fire = fireVideoDao.selectById(fireId);
        if (fire == null) {
            return ApiResponse.fail("火情记录不存在");
        }

        FireVideo.FireLevel levelEnum = parseFireLevel(fireLevel);
        if (levelEnum == null) {
            return ApiResponse.fail("无效的火情等级");
        }
        fire.setFireLevel(levelEnum);
        int updateResult = fireVideoDao.updateById(fire);

        // 2. 记录处理信息
        PersonFireHandling handling = new PersonFireHandling();
        handling.setPersonPhoneNumber(personPhoneNumber);
        handling.setFireId(fireId);
        handling.setHandleStatus("handled");
        int insertResult = personFireHandlingDao.insert(handling);

        return (updateResult > 0 && insertResult > 0)
                ? ApiResponse.success("处理成功")
                : ApiResponse.fail("处理过程中发生错误");
    }



    // 辅助方法：字符串转枚举（包含所有enum值判断）
    private FireVideo.FireLevel parseFireLevel(String fireLevel) {
        for (FireVideo.FireLevel level : FireVideo.FireLevel.values()) {
            if (level.getValue().equals(fireLevel)) {
                return level;
            }
        }
        return null;
    }

    @GetMapping("/getFireDescription")
    public ApiResponse getFireDescription(@RequestParam Integer fireId){
        return ApiResponse.success(fireVideoDao.selectFireExpansionInfo(fireId));
    }

    @GetMapping("/getFireDescriptions")
    public ApiResponse getFireDescriptions(){
        return ApiResponse.success(fireVideoDao.selectFireExpansionInfos());
    }

    @GetMapping("/fireHandleRecord")
    public ApiResponse fireHandleRecord(@RequestParam Integer fireId) {
        return ApiResponse.success(fireVideoDao.selectFireHandRecord(fireId));
    }

    // FireController.java 中新增接口
    @PutMapping("/add")
    public ApiResponse addFire(
            @RequestParam String videoPath,
            @RequestParam Float latitude,
            @RequestParam Float longitude) {

        // 参数校验逻辑
        if (videoPath == null || videoPath.isEmpty()) {
            return ApiResponse.fail("videoPath不能为空");
        }
        if (latitude == null || longitude == null) {
            return ApiResponse.fail("经纬度坐标不能为空");
        }

        // 构建实体
        FireVideo fireVideo = new FireVideo();
        fireVideo.setVideoPath(videoPath);
        fireVideo.setLatitude(latitude);
        fireVideo.setLongitude(longitude);
        fireVideo.setRecordTime(new Date());  // 使用服务器当前时间
        fireVideo.setFireLevel(FireVideo.FireLevel.unprocessed);

        // 插入数据库
        int result = fireVideoDao.insert(fireVideo);

        return result > 0
                ? ApiResponse.success("火情上传成功")
                : ApiResponse.fail("火情上传失败");
    }


}
